package net.lcheng.manage.controller;

import net.lcheng.commons.utils.PageList;
import net.lcheng.manage.vo.AttributeModel;
import net.lcheng.model.PCategoryAttribute;
import net.lcheng.model.PCategoryAttributeValue;
import net.lcheng.service.CategoryAttributeService;
import net.lcheng.service.CategoryAttributeValService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.UUID;

/**
 * Created by LV on 2016/6/27.
 */
@Controller
public class CategoryAttributeController {

    @Autowired
    private CategoryAttributeService categoryAttributeService;

    @Autowired
    private CategoryAttributeValService categoryAttributeValService;

    /**
     * 添加属性
     * @return
     */
    @RequestMapping(value = "/category/addAttribute/{categoryId}",method = RequestMethod.GET)
    public String addAttribute(Model model,@PathVariable("categoryId") String categoryId){
        //此处依赖分类业务
        model.addAttribute("categoryId",categoryId);
        return "category_attribute/category_attribute_add";
    }

    /**
     * 保存属性
     * @return
     */
    @RequestMapping(value = "/category/savingAttribute",method = RequestMethod.POST)
    public String savingAttribute(@Valid @ModelAttribute("AttributeModel") AttributeModel AttributeModel, BindingResult result){
        if(result.hasErrors()){
            return "/category_attribute/category_attribute_add";//返回添加属性页
        }

        PCategoryAttribute categoryAttribute=new PCategoryAttribute();
        categoryAttribute.setAttributeId(UUID.randomUUID().toString());
        categoryAttribute.setpCategoryCategoryId(AttributeModel.getCategoryId());
        categoryAttribute.setAttributeName(AttributeModel.getAttributeName());
//        categoryAttribute.setAttributeCode();//暂时不设置
        categoryAttribute.setAttributeType(AttributeModel.getAttributeType());
        categoryAttribute.setIsAllowedUpload(AttributeModel.getIsAllowedUpload());
        categoryAttribute.setOrderNum(AttributeModel.getOrderNum());
        categoryAttribute.setStatus(AttributeModel.getStatus());
        categoryAttribute.setIsAdaptAll(AttributeModel.getIsAdaptAll());
        categoryAttribute.setCreateOn(new Date());

        int status=categoryAttributeService.addAttribute(categoryAttribute);
        if(status>0&&AttributeModel.getAttributeValue()!=null&&AttributeModel.getAttributeValue()!="") {
            //属性值分割处理，用空格分离,分离后每个都要添加
            String[] attrValue = AttributeModel.getAttributeValue().split(" ");
            for (String tmp : attrValue) {
                PCategoryAttributeValue value = new PCategoryAttributeValue();
                value.setValueId(UUID.randomUUID().toString());
                value.setValueId(categoryAttribute.getAttributeId());
                value.setValueText(tmp);
                value.setCreateOn(new Date());
                categoryAttributeValService.addCategoryAttributeVal(value);
            }
        }


        return "redirect:/category/findAllAttributeByCategoryId/"+AttributeModel.getCategoryId();
    }

    /**
     * 查找当前的分类的所有属性
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/category/findAllAttributeByCategoryId/{categoryId}",method = RequestMethod.GET)
    public String findAllAttributeByCategoryId(Model model, @PathVariable("categoryId") String categoryId,
                                               @RequestParam(defaultValue = "1") int pageIndex){
        PageList<PCategoryAttribute> pageList=categoryAttributeService.
                findAllCategoryAttributeByCategoryId(categoryId,pageIndex,7);
        model.addAttribute("categoryId",categoryId);
        model.addAttribute("list",pageList.list);//当前页list
        model.addAttribute("pageTotal",pageList.totalPage);//总页数
        model.addAttribute("pageIndex",pageList.pageIndex);//当前页索引

        return "/category_attribute/category_attribute_list";
    }


    /**
     * 编辑分类属性
     * @param model
     * @param attributeId
     * @return
     */
    @RequestMapping(value = "/category/editCategoryAttribute/{attributeId}",method = RequestMethod.GET)
    public String editCategoryAttribute(Model model,@PathVariable("attributeId") String attributeId){
        PCategoryAttribute categoryAttribute=categoryAttributeService.findCategoryAttributeByPrimaryId(attributeId);
        model.addAttribute("categoryAttribute",categoryAttribute);
        return "/category_attribute/category_attribute_edit";
    }

    /**
     * 保存编辑分类属性
     * @return
     */
    @RequestMapping(value = "/category/savingEditCategoryAttribute",method = RequestMethod.POST)
    public String savingEditCategoryAttribute(@ModelAttribute("categoryAttribute") PCategoryAttribute categoryAttribute){
        categoryAttribute.setModifyOn(new Date());
        categoryAttributeService.updateCategoryAttributeByPrimaryId(categoryAttribute);
        return "redirect:/category/findAllAttributeByCategoryId/"+categoryAttribute.getpCategoryCategoryId();
    }

    /**
     * 删除分类属性
     * @return
     */
    @RequestMapping(value = "/category/deleteCategoryAttribute/{attributeId}",method = RequestMethod.GET)
    public String deleteCategoryAttribute(@PathVariable("attributeId") String attributeId){
        PCategoryAttribute categoryAttribute=categoryAttributeService.findCategoryAttributeByPrimaryId(attributeId);
        categoryAttributeService.deleteCategoryAttributeByPrimaryId(attributeId);
        return "redirect:/category/findAllAttributeByCategoryId/"+categoryAttribute.getpCategoryCategoryId();
    }
}
